package org.linkerproject.browser;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.eclipse.swt.widgets.Shell;

public class BrowserManager {

	public BrowserManager(Shell shell, String path) {
		this.path = path + "/http/";
		this.shell = shell;

		// operateWindow = new OperateWindow(shell, this);
		browserWindow = new BrowserWindow(shell, this);
		browserWindow.browse("http://google.com");

	}

	public void browse(String url) {
		browserWindow.browse(url);
	}

	// public void openOperate() {
	// operateWindow.open();
	// }

	private BrowserWindow browserWindow;

	private String path;

	private Shell shell;

	public boolean isWebUrl(String location) {
		String url = location.trim();
		if (url.indexOf("localhost") >= 0 || url.indexOf("172.0.0.1") >= 0) {
			return false;
		}

		return true;
	}

	// public void perate(String url) {
	// operateWindow.setUrl(server.getLocalUrl() + url);
	// }

	public void gotoDialog() {
		UrlDialog dialog = new UrlDialog(shell, this);
		dialog.open();
	}

	public void findDialog() {
		SearchDialog dialog = new SearchDialog(shell,this);
		dialog.open();
	}

	public void save(String lastUrl, String html) {

		System.out.println("svaing:" + lastUrl);
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(lastUrl));
			bw.write(html);
			bw.flush();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getTitle() {
		return browserWindow.getTitle();
	}

	public void browseLocal(String file) {
		this.browse(path + file);
	}

}
